package com.example.pulseanalyse.veinType;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yyw
 * time: 2020/12/8 14:40
 * desc: list 分组工具
 */
public class GroupingUtil {
    /**
     * 根据类型分组
     *
     * @param list
     * @param comparator
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> dividerList(List<T> list, Comparator<? super T> comparator) {
        List<List<T>> lists = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            boolean isContain = false;
            for (int j = 0; j < lists.size(); j++) {
                if (lists.get(j).size() == 0 || comparator.compare(lists.get(j).get(0), list.get(i)) == 0) {
                    lists.get(j).add(list.get(i));
                    isContain = true;
                    break;
                }
            }
            if (!isContain) {
                List<T> newList = new ArrayList<>();
                newList.add(list.get(i));
                lists.add(newList);
            }
        }
        return lists;
    }

    /**
     * 根据string分组
     *
     * @param list
     * @return
     */
    public static List<List<String>> dividerStrList(List<String> list) {
        List<List<String>> lists = new ArrayList<>();
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        for (int i = 0; i < list.size(); i++) {
            boolean isContain = false;
            for (int j = 0; j < lists.size(); j++) {
                if (lists.get(j).size() == 0 || comparator.compare(lists.get(j).get(0), list.get(i)) == 0) {
                    lists.get(j).add(list.get(i));
                    isContain = true;
                    break;
                }
            }
            if (!isContain) {
                List<String> newList = new ArrayList<>();
                newList.add(list.get(i));
                lists.add(newList);
            }
        }
        return lists;
    }
    /**
     * 根据string分组返回map类型
     *
     * @param list
     * @return
     */
    public static Map<String, List<String>> dividerStrList2(List<String> list) {
        Map<String, List<String>> map = new HashMap<>();
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };
        for (int i = 0; i < list.size(); i++) {
            boolean isContain = false;

            for (String key : map.keySet()) {
                if (map.size() == 0 || comparator.compare(key, list.get(i)) == 0) {
                    map.get(key).add(list.get(i));
                    isContain = true;
                    break;
                }
            }

            if (!isContain) {
                List<String> newList = new ArrayList<>();
                newList.add(list.get(i));
                map.put(list.get(i), newList);
            }
        }
        return map;
    }
}
